package com.eet.rabbitmq.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

/**
 * 生产者
 *
 * @author zheng.zhang
 */
public class Producer {

    public static void main(String[] args) {
        final ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("www.eet.ink");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");

        try (final Connection connection = factory.newConnection();
             final Channel channel = connection.createChannel()) {
            String exchangeName = "topic.course.exchange";
            String type = "topic";
            channel.exchangeDeclare(exchangeName, type, true);

            String queue7 = "queue7";
            String queue8 = "queue8";
            String queue9 = "queue9";
            channel.queueDeclare(queue7, true, false, false, null);
            channel.queueDeclare(queue8, true, false, false, null);
            channel.queueDeclare(queue9, true, false, false, null);

            channel.queueBind(queue7, exchangeName, "course.#");
            channel.queueBind(queue8, exchangeName, "#.order.#");
            channel.queueBind(queue9, exchangeName, "#.user.*");

            String message = "topic消息测试!";
            channel.basicPublish(exchangeName, "course.user.order.test", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());

            System.out.println("消息发送成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
